class CategoriesController < ApplicationController

  before_filter :login_required
  before_filter :initialize_category, :only => [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.xml
  def index
    @page_title = "Categories"
    @categories = @current_user.categories.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @categories }
      format.iphone do # index.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
    @page_title = "New Category"
    @category = @current_user.categories.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
      format.iphone do # new.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # POST /categories
  # POST /categories.xml
  def create
    @category = @current_user.categories.create(params[:category])

    respond_to do |format|
      if @category.save
        flash[:notice] = "Category was successfully created."
        format.html { redirect_to(@category) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
        format.iphone { redirect_to(@category) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
        format.iphone { render :action => "new" }
      end
    end
  end

  # GET /categories/1/edit
  def edit
  end

  # GET /categories/1
  # GET /categories/1.xml
  def show
    @page_title = "Category: " + @category.name

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @category }
      format.iphone do # show.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    respond_to do |format|
      if @category.update_attributes(params[:category])
        flash[:notice] = "Category was successfully updated."
        format.html { redirect_to(@category) }
        format.xml  { head :ok }
        format.iphone { redirect_to :controller => "welcome", :action => "home" }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
        format.iphone { render :action => "edit" }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
      format.iphone { redirect_to(categories_url) }
    end
  end

private
  def initialize_category()
    if logged_in?
      @category = @current_user.categories.find(params[:id])
      head 404 unless @category
    else
      flash[:warning] = "You must be logged in to perform that operation."
      redirect_to new_sessions_path
    end
  end

end
